package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加购物车");
//        通过线程获取当前用户id
        Long currentId = BaseContext.getCurrentIdUser();
//        设置用户id，指定当前是哪个用户的购物车数据
        shoppingCart.setUserId(currentId);
//        获取菜品id
        Long dishId = shoppingCart.getDishId();
//        条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        构造用户条件
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
//        对菜品id进行判断
        if (null != dishId) {
            //        构造菜品条件
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //        否则构造套餐条件
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

//        对购物车中商品进行非空判断
        if (null != cartServiceOne) {
            //如果已经存在，就在原来数量基础上加一
//        获取当前数量
            Integer number = cartServiceOne.getNumber();
//        在获取的数量上加1
            cartServiceOne.setNumber(number + 1);
//        对购物车进行更新操作
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
//        设置数量为1
           shoppingCart.setNumber(1);
//        设置创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
//        进行添加操作
            shoppingCartService.save(shoppingCart);
//        把添加后的数据给到购物车
            cartServiceOne = shoppingCart;
        }
        //        返回结果
        return R.success(cartServiceOne);


    }
    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentIdUser());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }
    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentIdUser());

        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
    @PostMapping("/sub")
    public R<String> remove(@RequestBody ShoppingCart shoppingCart){
        log.info("前端传回的数据:{}",shoppingCart);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId()).or().eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        shoppingCartService.remove(queryWrapper);
        return R.success("移除成功");
    }

}
